package cn.wanda.bi.sunshine.platform.server.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.FactUserTransMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.FactUserTrans;
import cn.wanda.bi.sunshine.platform.server.bean.model.FactUserTransExample;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;

/**
 * Created by wangfupeng1
 */
@Service
public class FactUserTransService {

	public static final Logger logger = LoggerFactory.getLogger(FactUserTransService.class);

	@Autowired
	private FactUserTransMapper factUserTransMapper;

	public ListResponse<FactUserTrans> list(Integer index, Integer limit, String memMobile, String productName,
			String storeId, String storeName, Integer plazaId, String plazaName, String level1Name, String level3Name,
			String categoryName1, String categoryName2, Long cashAmountTransStart, Long cashAmountTransEnd, Integer productCntsStart, Integer productCntsEnd, String consumeType,
			Long consumeDtStart, Long consumeDtEnd) {
		FactUserTransExample factUserTransExample = new FactUserTransExample();
		FactUserTransExample.Criteria criteria = factUserTransExample.createCriteria();
		if (StringUtils.isNotEmpty(memMobile))
			criteria.andMemMobileEqualTo(memMobile);
		if (StringUtils.isNotEmpty(productName)) {
			criteria.andProductNameEqualTo(productName);
		}
		if (StringUtils.isNotEmpty(storeId)) {
			criteria.andStoreIdEqualTo(storeId);
		}
		if (StringUtils.isNotEmpty(storeName)) {
			criteria.andStoreNameEqualTo(storeName);
		}
		if (plazaId!=null && StringUtils.isNotEmpty(plazaId+"")) {
			criteria.andPlazaIdEqualTo(plazaId);
		}
		if (StringUtils.isNotEmpty(plazaName)) {
			criteria.andPlazaNameEqualTo(plazaName);
		}
		if (StringUtils.isNotEmpty(level1Name)) {
			criteria.andLevel1NameEqualTo(level1Name);
		}
		if (StringUtils.isNotEmpty(level3Name)) {
			criteria.andLevel3NameEqualTo(level3Name);
		}
		if (StringUtils.isNotEmpty(categoryName1)) {
			criteria.andCategoryName1EqualTo(categoryName1);
		}
		if (StringUtils.isNotEmpty(categoryName2)) {
			criteria.andCategoryName2EqualTo(categoryName2);
		}
		if (cashAmountTransStart!=null && StringUtils.isNotEmpty(cashAmountTransStart+"")) {
			criteria.andCashAmountTransGreaterThan(BigDecimal.valueOf(cashAmountTransStart));
		}
		if (cashAmountTransEnd!=null && StringUtils.isNotEmpty(cashAmountTransEnd+"")) {
			criteria.andCashAmountTransLessThan(BigDecimal.valueOf(cashAmountTransEnd));
		}
		if (productCntsStart!=null && StringUtils.isNotEmpty(productCntsStart+"")) {
			criteria.andProductCntsGreaterThan(productCntsStart);
		}
		if (productCntsEnd!=null && StringUtils.isNotEmpty(productCntsEnd+"")) {
			criteria.andProductCntsLessThan(productCntsEnd);
		}
		if (StringUtils.isNotEmpty(consumeType)) {
			criteria.andConsumeTypeEqualTo(consumeType);
		}
		if (consumeDtStart!=null && StringUtils.isNotEmpty(consumeDtStart+"")) {
			criteria.andConsumeDtGreaterThan(consumeDtStart+"");
		}
		if (consumeDtEnd!=null && StringUtils.isNotEmpty(consumeDtEnd+"")) {
			criteria.andConsumeDtLessThan(consumeDtEnd+"");
		}

		factUserTransExample.setOffset(index * limit - limit);
		factUserTransExample.setLimit(limit);
		List<FactUserTrans> factUserTranss = factUserTransMapper.selectByExample(factUserTransExample);
		Integer count = factUserTransMapper.countByExample(factUserTransExample);
		return ListResponse.success(factUserTranss, count, index, limit);
	}

}
